package com.mdwz.lu.ukulelecomposer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;


public class CompositionManager extends Activity {

    ArrayList< Composition > lstCompositionMgr;
    ListView listview;
    ArrayList<Map<String,Object >> lvdata;
    SimpleAdapter lvadapter;

    static int idCount = 0;
    static public String intentkeys [] = { "title" , "composer" , "songwriter" ,
            "keysig" , "timesig"};
    static public String namekeys [] = { "title" , "composer" , "keysignature" ,
            "timesignature" , "createdate"};


    public boolean LoadCompositionDatabase() {

        lstCompositionMgr = new ArrayList< Composition >();
        return true;

    }

    public boolean InitListView( ListView lv ){

        int ids[] = { R.id.title , R.id.composer , R.id.keysignature ,
                R.id.timesignature , R.id.createdate };


        lvdata = new ArrayList< Map<String,Object>>();

        for ( int i = 0 ; i < lstCompositionMgr.size() ; i ++ ) {

            Map< String, Object> map = new HashMap<String, Object>();

            map.put( namekeys[0] , lstCompositionMgr.get(i).title);
            map.put( namekeys[1] , lstCompositionMgr.get(i).composer);
            map.put( namekeys[2] , lstCompositionMgr.get(i).keysig );
            map.put( namekeys[3] , lstCompositionMgr.get(i).timesig );
            map.put( namekeys[4] , lstCompositionMgr.get(i).createdate );
            map.put( "id"    , lstCompositionMgr.get(i).id );

            lvdata.add( map );
        }

        lvadapter = new SimpleAdapter(this , lvdata , R.layout.item_listview_composition_manager ,
                namekeys , ids );

        lv.setAdapter( lvadapter );
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_composition_manager);

        LoadCompositionDatabase();

        listview = (ListView)this.findViewById( R.id.listView);

        InitListView( listview );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.composition_manager, menu);
        return true;
    }

    public final static int NEW_COMPOSITION_REQUEST = 1;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.new_composition) {

            Intent intent = new Intent();
            intent.setClass(this , NewComposition.class );
            startActivityForResult( intent , NEW_COMPOSITION_REQUEST );
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if ( requestCode == NEW_COMPOSITION_REQUEST ){
            if ( resultCode == RESULT_OK ){

                Composition compo = new Composition();
                compo.id = idCount ++;
                compo.title = data.getStringExtra( intentkeys[0] );
                compo.composer = data.getStringExtra( intentkeys[1]);
                compo.songwriter = data.getStringExtra( intentkeys[2]);
                compo.keysig = data.getStringExtra( intentkeys[3]);
                compo.timesig = data.getStringExtra( intentkeys[4]);
                compo.createdate = new Date();

                lstCompositionMgr.add( compo );

                Map< String, Object> map = new HashMap<String, Object>();

                map.put( namekeys[0] , compo.title);
                map.put( namekeys[1] , compo.composer);
                map.put( namekeys[2] , compo.keysig );
                map.put( namekeys[3] , compo.timesig );
                map.put( namekeys[4] , compo.createdate );
                map.put( "id"    , compo.id );

                lvdata.add( map );
                lvadapter.notifyDataSetChanged();
            }
        }
    }
}
